//
//  HDTextField.m
//  AccountEasy
//
//  Created by mac on 2020/7/24.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "HDTextField.h"

@interface HDTextField ()

@property (nonatomic, assign) int rightSpacing;
@property (nonatomic, assign) int count;
@property (nonatomic, strong) UIButton *validationBtn;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation HDTextField

+(instancetype)initWithPlaceholder:(NSString *)placeholder AndPoint:(CGPoint)point; {
    
    HDTextField * textField = [HDTextField new];
    textField.frame = SARectMake(point.x, point.y, 321, 71);
    textField.placeholder = placeholder;
    textField.rightSpacing = 0;
    UIImage *textFieldBgImage = [[UIImage imageNamed:@"roundedRectangle"] stretchableImageWithLeftCapWidth:30 topCapHeight:0];
    [textField setBackground:textFieldBgImage];
    
    return textField;
}

+(instancetype)initWithEyeAndPlaceholder:(NSString *)placeholder  AndPoint:(CGPoint)point {
    
    HDTextField * textField = [HDTextField new];
    textField.frame = SARectMake(point.x, point.y, 321, 71);
    textField.placeholder = placeholder;
    textField.rightSpacing = 25;
    textField.secureTextEntry = YES;
    
    UIImage *textFieldBgImage = [[UIImage imageNamed:@"roundedRectangle"] stretchableImageWithLeftCapWidth:30 topCapHeight:0];
    [textField setBackground:textFieldBgImage];
    
    UIView *rightView = [UIView new];
    rightView.frame = SARectMake(0, 0, 60, 71);
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"eye_0"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"eye_1"] forState:UIControlStateSelected];
    [rightBtn addTarget:textField action:@selector(clickedEyeBtn:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = SARectMake(0, 0, 42, 71);
    
    [rightView addSubview:rightBtn];
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
    return textField;
}

+(instancetype)initWithValidationAndPlaceholder:(NSString *)placeholder  AndPoint:(CGPoint)point {
    
    HDTextField * textField = [HDTextField new];
    textField.frame = SARectMake(point.x, point.y, 321, 71);
    textField.placeholder = placeholder;
    textField.rightSpacing = 90;
    
    UIImage *textFieldBgImage = [[UIImage imageNamed:@"roundedRectangle"] stretchableImageWithLeftCapWidth:30 topCapHeight:0];
    [textField setBackground:textFieldBgImage];
    
    UIView *rightView = [UIView new];
    rightView.frame = SARectMake(0, 0, 120, 71);
    
    UIView * line = [UIView new];
    line.frame = SARectMake(0, 19, 1, 35);
    line.backgroundColor = kLineColor;
    [rightView addSubview:line];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [rightBtn addTarget:textField action:@selector(clickedValidationBtn:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = SARectMake(15, 0, 80, 71);
    rightBtn.titleLabel.font = kFont(15);
    [rightBtn setTitleColor:kColor(80, 80, 80, 1) forState:UIControlStateNormal];
    
    [rightView addSubview:rightBtn];
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
    textField.validationBtn = rightBtn;
    if (@available(iOS 12.0, *)) {
        textField.textContentType = UITextContentTypeOneTimeCode;
    } else {
        if (@available(iOS 10.0, *)) {
            textField.textContentType = @"one-time-code";
        } else {
            // Fallback on earlier versions
        }
    }
    
    return textField;
    
}

-(void)clickedEyeBtn:(UIButton *)sender {
    self.secureTextEntry = !self.isSecureTextEntry;
    sender.selected = !self.isSecureTextEntry;
}

-(void)clickedValidationBtn:(UIButton *)sender {
    if (self.validationBlock) {
        self.validationBlock();
    }
}

-(void)startTime {
    self.validationBtn.userInteractionEnabled = NO;
    [self.validationBtn setTitle:[NSString stringWithFormat:@"剩余60秒"] forState:UIControlStateNormal];
    self.count = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
        [self.validationBtn setTitle:[NSString stringWithFormat:@"剩余%d秒", self.count] forState:UIControlStateNormal];
    } else {
        self.validationBtn.userInteractionEnabled = YES;
        [self.validationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        // 停掉定时器
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect inset = CGRectMake(bounds.origin.x + kScaleNum(30), bounds.origin.y, bounds.size.width - kScaleNum(60 + self.rightSpacing), bounds.size.height);
    return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGRect inset = CGRectMake(bounds.origin.x + kScaleNum(30), bounds.origin.y, bounds.size.width - kScaleNum(60 + self.rightSpacing), bounds.size.height);
    return inset;
}

@end
